//
//  rootTableViewController.m
//  UI_stroy bored版
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 mjh. All rights reserved.
//

#import "rootTableViewController.h"
#import "OnlineMovieModel.h"
#import "MovieDitailTableViewCell.h"
#import "MovieDetialViewController.h"

@interface rootTableViewController ()

@property (nonatomic, strong)OnlineMovieModel *onlineMovieModel;

@end

@implementation rootTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self sendRequest];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)sendRequest {
    
    NSString *string = @"http://api.douban.com/v2/movie/nowplaying";
    NSURL *url = [NSURL URLWithString:string];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    
    
    NSString *postString = @"app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=0&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a";
    
    NSData *body = [postString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:body];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//            NSLog(@"%@", result);
            
            self.onlineMovieModel = [[OnlineMovieModel alloc] initWithDataSource:result];
            NSLog(@"%@", self.onlineMovieModel.total);
//            网络数据返回的时候刷新tableView
           
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
        
    }];
    [task resume];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.onlineMovieModel.entries.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieDitailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MovieDEtailCellIdentifier" forIndexPath:indexPath];
    MovieDetailModel *movieDetailModel = self.onlineMovieModel.entries[indexPath.row];
    cell.movieDetailModel = movieDetailModel;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
//    取消cell选中点击效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
   
    
    MovieDetailModel *mocel = self.onlineMovieModel.entries[indexPath.row];
    
     MovieDetialViewController *movieDetialViewController = [[MovieDetialViewController alloc]initWithMovieDetailModel:mocel];
    
    
    [self.navigationController pushViewController:movieDetialViewController animated:YES];
    
    
    
}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
